<?php

namespace App\Http\Controllers\QingShanSupplier;

use Validator;
use App\Customize\Func;
use App\Customize\Sign;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Models\QingShanERP\SupplierStaff;

class LoginController extends Controller
{
    // 登录
    public function postLogin(Request $req)
    {
        try {
            // 先验证签名
            $res = Sign::aes_decrypt($req->all());
            if ($res['code'] != 200) {
                return $this->resData(['code' => 403, 'msg' => $res['msg'] . '...', 'data' => []]);
            }
            // 合并解析到的参数进请求中
            $req->merge($res['data']);
            $validator = Validator::make($req->input(), [
                'name' => 'required|min:2|max:15',
                'password' => 'required|min:6|max:15',
            ]);
            $attrs = array(
                'name' => '用户名',
                'password' => '密码',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $username = $req->input('name');
            $pwd = $req->input('password');
            $user = SupplierStaff::where('status', 1)->where('del_flag',0)->where('phone', $username)->first();
            if (is_null($user)) {
                return $this->resData(400, '用户不存在或已被禁用...');
            } else {
                if ($user->password != Func::makepwd($pwd, $user->crypt)) {
                    return $this->resData(400, '密码不正确...');
                }
                // 更新一些信息
                SupplierStaff::where('id', $user->id)->update(['lasttime' => date('Y-m-d H:i:s'), 'lastip' => $req->ip()]);
                $token = md5(md5($user->id . '-SUPPLIER-' . $user->staffname));
                // 放到redis里边，先删除旧的，有效期一天
                Redis::del('s-token:' . $token);
                Redis::setex('s-token:' . $token, 3600 * 24, json_encode(['id' => $user->id, 'name' => $user->staffname]));
                $res = ['id' => $user->id, 'name' => $user->staffname, 'stoken' => $token];
                return $this->resData(200, '登录成功！', $res);
            }
        } catch (\Throwable $e) {
            return $this->resData(500, '获取失败，请稍后再试...', $e->getMessage());
        }
    }
}
